import 'package:deer/login/login_router.dart';
import 'package:deer/res/colors.dart';
import 'package:deer/res/styles.dart';
import 'package:deer/routers/fluro_navigator.dart';
import 'package:deer/util/utils.dart';
import 'package:deer/widgets/app_bar.dart';
import 'package:deer/widgets/text_field.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:flutter/foundation.dart';
import 'package:deer/store/store_router.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _pwdController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  FocusScopeNode scopeNode;

  bool _isClick = false;

  @override
  void initState() {
    super.initState();
    // 监听输入改变
    _nameController.text = '15626108393';
    _pwdController.text = '123456';
    _nameController.addListener(_verify);
    _pwdController.addListener(_verify);
  }

  void _verify() {
    String name = _nameController.text;
    String password = _pwdController.text;
    bool isClick = true;
    if (name.isEmpty || name.length < 11) {
      isClick = false;
    }
    if (password.isEmpty || password.length < 6) {
      isClick = false;
    }

    if (isClick != _isClick) {
      setState(() {
        _isClick = isClick;
      });
    }
  }

  void _login() {
    NavigatorUtils.push(context, StoreRouter.auditPage);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        isBack: false,
        actionName: '验证码登录',
        onPressed: () {
          NavigatorUtils.push(context, LoginRouter.smsLoginPage);
        },
      ),
      body: defaultTargetPlatform == TargetPlatform.iOS
          ? FormKeyboardActions(
              child: _buildBody(),
            )
          : SingleChildScrollView(
              child: _buildBody(),
            ),
    );
  }

  _buildBody() {
    return GestureDetector(
      onTap: () {
        if (scopeNode == null) {
          scopeNode = FocusScope.of(context);
        }
        _nodeText1.unfocus();
        _nodeText2.unfocus();
      },
      child: Container(
        color: Colors.white,
        child: Padding(
          padding: EdgeInsets.only(top: 20, left: 16, right: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                '密码登录',
                style: TextStyles.textBoldDark26,
              ),
              Gaps.vGap(16),
              MyTextField(
                controller: _nameController,
                hintText: '请输入账号',
                keyboardType: TextInputType.phone,
                maxLength: 11,
                focusNode: _nodeText1,
              ),
              Gaps.vGap(10),
              MyTextField(
                controller: _pwdController,
                hintText: '请输入密码',
                isInputPwd: true,
                focusNode: _nodeText2,
                config:
                    Utils.getKeyboardActionsConfig([_nodeText1, _nodeText2]),
              ),
              Gaps.vGap(24),
              Container(
                width: double.infinity,
                height: 44,
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(3)),
                  color: Colours.app_main,
                  textColor: Colors.white,
                  disabledTextColor: Colours.login_text_disabled,
                  disabledColor: Colours.login_button_disabled,
                  child: Text('登录', style: TextStyle(fontSize: 18)),
                  onPressed: _isClick ? _login : null,
                ),
              ),
              Container(
                height: 40,
                alignment: Alignment.centerRight,
                child: GestureDetector(
                  child: Text(
                    '忘记密码',
                    style: TextStyles.textGray12,
                  ),
                  onTap: () {
                    NavigatorUtils.push(context, LoginRouter.resetPasswordPage);
                  },
                ),
              ),
              Container(
                  alignment: Alignment.center,
                  height: 18,
                  child: GestureDetector(
                    child: Text('还没账号？快去注册', style: TextStyles.textMain14),
                    onTap: () {
                      NavigatorUtils.push(context, LoginRouter.registerPage);
                    },
                  ))
            ],
          ),
        ),
      ),
    );
  }
}
